package JUC.AQS.DebugDemo;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author CKC
 * @DateTime 2021/7/24 19:30
 */
public class DeBugReentrantReadWriteLockDemo {
    private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    private static final Condition condition = lock.writeLock().newCondition();
    
    public static void main(String[] args) {
        new Thread(DeBugReentrantReadWriteLockDemo::demo, "t0").start();
        new Thread(DeBugReentrantReadWriteLockDemo::demo1, "t1").start();
        new Thread(DeBugReentrantReadWriteLockDemo::demo2, "t2").start();
        new Thread(DeBugReentrantReadWriteLockDemo::demo3, "t3").start();
    }
    
    public static void demo() {
        lock.writeLock().lock();
        try {
            condition.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.writeLock().unlock();
        }
    }
    
    public static void demo1() {
        lock.writeLock().lock();
        try {
            condition.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.writeLock().unlock();
        }
    }
    
    public static void demo2() {
        lock.readLock().lock();
        try {
        }finally {
            lock.readLock().unlock();
        }
    }
    
    public static void demo3() {
        lock.readLock().lock();
        try {
            condition.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.readLock().unlock();
        }
    }
    
}
